package com.A01.sys.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.A01.sys.entity.EmployeeEntity;
import com.A01.sys.entity.RewardEntity;
import com.A01.sys.entity.UserEntity;
import com.A01.sys.service.EmployeeService;
import com.A01.sys.service.RewardService;
import com.A01.utils.ContextHolderUtils;
import com.A01.utils.RRException;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.A01.utils.PageUtils;
import com.A01.utils.R;



/**
 * 奖惩信息控制器。
 *
 */
@RestController
@RequestMapping("dev/reward")
public class RewardController {
    @Autowired
    private RewardService rewardService;
    @Autowired
    private EmployeeService employeeService;

    /**
     * 根据用户信息，打开列表。
     */
    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> params){
        PageUtils page = rewardService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 根据身份证号查询奖惩列表
     */
    @RequestMapping("/employeeRewardList/{idNumber}")
    public R employeeRewardList(@PathVariable("idNumber") String idNumber){
        List<RewardEntity> list = rewardService.selectList(new EntityWrapper<RewardEntity>().eq("id_number", idNumber));
        return R.ok().put("list", list);
    }

    /**
     * 返回奖惩信息。
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        RewardEntity reward = rewardService.selectById(id);

        return R.ok().put("reward", reward);
    }

    /**
     * 保存员工的奖惩记录。
     */
    @RequestMapping("/save")
    public R save(@RequestBody RewardEntity reward){
        EmployeeEntity employeeEntity = employeeService.selectById(reward.getEmployeeId());
        if (employeeEntity == null) {
            throw new RRException("员工不存在");
        }
        if(reward.getCreateDate() == null){
            throw new RRException("创建日期不能为空");
        }
        if(reward.getType() == null){
            throw new RRException("奖惩类型不能为空");
        }
        if(reward.getDescribe() == null){
            throw new RRException("奖惩描述不能为空");
        }
        UserEntity user = ContextHolderUtils.getUser();
        reward.setCompanyId(user.getCompanyId());
        reward.setIdNumber(employeeEntity.getIdNumber());
        rewardService.insert(reward);
        return R.ok();
    }

    /**
     * 修改记录。
     */
    @RequestMapping("/update")
    public R update(@RequestBody RewardEntity reward){
        EmployeeEntity employeeEntity = employeeService.selectById(reward.getEmployeeId());
        if (employeeEntity == null) {
            throw new RRException("员工不存在");
        }
        if(reward.getCreateDate() == null){
            throw new RRException("创建日期不能为空");
        }
        if(reward.getType() == null){
            throw new RRException("奖惩类型不能为空");
        }
        if(reward.getDescribe() == null){
            throw new RRException("奖惩描述不能为空");
        }
        rewardService.updateAllColumnById(reward);//全部更新

        return R.ok();
    }

    /**
     * 批量删除记录
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        rewardService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

}
